package com.doyens.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doyens.auth.mapper.SysMenuMapper;
import com.doyens.auth.service.SysMenuService;


import com.doyens.common.config.utils.MenuHelper;
import com.doyens.execption.DoyensException;
import com.doyens.model.system.SysMenu;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysMenuServiceImpl  extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {
    @Override
    public List<SysMenu> findNodes() {
////        查询所有菜单
      LambdaQueryWrapper<SysMenu> wrapper= new LambdaQueryWrapper<>();
      wrapper.eq(SysMenu::getStatus,1);
        List<SysMenu> list = this.list(wrapper);
//        根据这个所有的菜单构建出我们要的树型结构的菜单
        List<SysMenu> sysMenuList= MenuHelper.buildTree(list);
        return sysMenuList;
    }

    @Override
    public void removeMenuById(int id) {
//        先判断是否有孩子气
          LambdaQueryWrapper<SysMenu> wrapper=new LambdaQueryWrapper<>();
          wrapper.eq(SysMenu::getParentId,id);
        int count = this.count(wrapper);
        if(count==0){
            throw new DoyensException(204,"该菜单下还有其他的子菜单，不能删除");
        }
        this.removeById(id);
    }
}
